package routerServer

import (
	"fmt"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/FileName"
	"go-fox-edge-iot-gateway/commEnv"
	"log"
	"net/http"
)

type routerServer struct {
}

func (e *routerServer) startServer(certFile, keyFile string) {
	// 注册路由
	router.registerRouter()

	if certFile == "" || keyFile == "" {
		// 启动服务
		serverPort := fmt.Sprintf(":%v", commEnv.Service.Port)
		fmt.Println("start http server " + serverPort)

		if err := engine.Run(serverPort); err != nil {
			log.Fatalf("Failed to start http server: %v", err)
		}
	} else {
		// 使用HTTPS监听443端口
		serverPort := fmt.Sprintf(":%v", commEnv.Service.Port)
		fmt.Println("start https server " + serverPort)

		certFile = FileName.GetOsFilePath(commEnv.Service.WorkDir + "/" + certFile)
		keyFile = FileName.GetOsFilePath(commEnv.Service.WorkDir + "/" + keyFile)

		if err := http.ListenAndServeTLS(serverPort, certFile, keyFile, engine); err != nil {
			log.Fatalf("Failed to start https server: %v", err)
		}
	}
}
